package com.personal.common.util;

public class SpecialCharUtil {

	// 对特殊字符转义
	public static String html_encode(String str) {
		if (str == null || "".equals(str)) {
			return "";
		}
		String str_encode;
		str_encode = str.replace("&", "&amp;");
		str_encode = str_encode.replace("<", "&lt;");
		str_encode = str_encode.replace(">", "&gt;");
		str_encode = str_encode.replace(" ", "&nbsp;");
		str_encode = str_encode.replace("'", "&#39;");
		str_encode = str_encode.replace("\"", "&quot;");
		str_encode = str_encode.replace("\\n", "<br>");
		return str_encode;
	}

	// 对特殊字符解码
	public static String html_decode(String str) {
		if (str == null || "".equals(str)) {
			return "";
		}
		String str_decode;
		str_decode = str.replace("&amp;", "&");
		str_decode = str_decode.replace("&lt;", "<");
		str_decode = str_decode.replace("&gt;", ">");
		str_decode = str_decode.replace(" ", "&nbsp;");
		str_decode = str_decode.replace("&#39;", "'");
		str_decode = str_decode.replace("&quot;", "\"");
		str_decode = str_decode.replace("<br>", "\n");
		return str_decode;
	}
}
